package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.MyAssert;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.netty.util.internal.StringUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    SystemUserService systemUserService;


    @Override
    public void saveCaptcha(String key, String lowerCase) {
        redisTemplate.opsForValue().set(key, lowerCase,60, TimeUnit.MINUTES);

    }

    @Override
    public String login(LoginVo loginVo) {
        String captchaKey = loginVo.getCaptchaKey();
        // 脚本text
        String luaScript = "local value = redis.call(\"get\",KEYS[1]) if KEYS[1] ~= nil then redis.call(\"del\",KEYS[1]) end return value ";//redis.call("del",KEYS[1])

        // 脚本
        DefaultRedisScript<String> redisScript = new DefaultRedisScript<>();
        redisScript.setResultType(String.class);
        redisScript.setScriptText(luaScript);

        // 执行

        String result = redisTemplate.execute(redisScript, Arrays.asList(captchaKey), "");



        String captchaCode = loginVo.getCaptchaCode();


        redisTemplate.delete(captchaKey);
        MyAssert.isTrue(StringUtils.hasText(result) && result.equals(captchaCode), "验证码错误");

        System.out.println(DigestUtils.md5Hex(loginVo.getPassword()));
        //验证用户名跟密码
        SystemUser user = systemUserService
                .getOne(new QueryWrapper<SystemUser>()
                        .eq("username",
                                loginVo.getUsername())
                        .eq("password",
                                DigestUtils.md5Hex(loginVo.getPassword())));
        MyAssert.notNull(user, "用户名错误");

        //生成token
        String token = JwtUtil.createToken(user.getId(), user.getUsername());

        return token;
    }



    @Override
    public SystemUserInfoVo getUserInfoById(String id) {
        SystemUser user = systemUserService.getById(id);
        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setName(user.getName());
        systemUserInfoVo.setAvatarUrl(user.getAvatarUrl());
        return systemUserInfoVo;
    }
}
